<template>
	<div class="list"
				ref="wrapper"
		>
		<div>
			<div class="area border-topbottom">
				<div class="title">当前城市</div>
				<div class="button-list">
					<div class="button-wrapper">
						<div class="button">{{this.currentCity}}</div>
					</div>
				</div>
			</div>
			<div class="area border-topbottom">
				<div class="title">热门城市</div>
				<div class="button-list">
					<div class="button-wrapper" v-for="item of hot" :key="item.id" @click="handleCityClick(item.name)">
						<div class="button">{{item.name}}</div>
					</div>
				</div>
			</div>
			<div class="area" v-for="(item, key) of cities" :key="key" :ref="key">
				<div class="title border-topbottom">{{key}}</div>
				<div class="item-list">
					<div class="item border-bottom" v-for="innerItem of item" :key="innerItem.id"  @click="handleCityClick(innerItem.name)">{{innerItem.name}}</div>
				</div>
			</div>
		</div>
	</div>
</template>

<script>
import Bscroll from 'better-scroll'
import { mapState, mapMutations } from 'vuex'
export default {
  name: 'CityList',
  props: {
    cities: Object,
    hot: Array,
    letter: String
  },
  data () {
    return {
      touchStatus: false,
      floorHeight: [],
      scrollY: 0
    }
  },
  computed: {
    ...mapState({
      currentCity: 'city'
    })
  },
  methods: {
    handleCityClick (city) {
      // this.$store.dispatch('changeCity', city)
      // this.$store.commit('changeCity', city)
      this.changeCity(city)
      // 回到首页
      this.$router.push('/')
    },
    ...mapMutations(['changeCity']),
    _getHeight () {
      let keys = []
      let height = 0
      // 循环对象
      for (let key in this.cities) {
        keys.push(key)
      }
      this.floorHeight.push(height)
      // 获取每个cities的高度
      for (let i = 0; i < keys.length; i++) {
        height += this.$refs[keys[i]][0].clientHeight
        this.floorHeight.push(height)
      }
    },
    _getIndex () {
      for (let i = 0; i < this.floorHeight.length; i++) {
        let height1 = this.floorHeight[i]
        let height2 = this.floorHeight[i + 1]
        if (!height2 || (this.scrollY >= height1 && this.scrollY < height2)) {
          return i
        }
      }
      return 0
    }
  },
  watch: {
    letter () {
      if (this.letter) {
        const element = this.$refs[this.letter][0]
        this.scroll.scrollToElement(element)
      }
    }
  },
  mounted () {
    this.scroll = new Bscroll(this.$refs.wrapper, {
      click: true,
      probeType: 3
    })
    this.scroll.on('scroll', (pos) => {
      this.scrollY = Math.abs(Math.round(pos.y) + 182) // 182是当前城市和热门城市的高度
      this.floorHeight = [] // 每一次清空城市列表高度的数组
      this._getHeight() // 然后再获取城市列表高度的数组
      let index = this._getIndex() // 获取当前城市高度数组的索引值
      this.$emit('change', index) // 通过change传递给父元素
    })
  }
}
</script>

<style lang="stylus" scoped>
@import '~styles/varibles.styl'
.border-topbottom
	&:before
		border-color: #ccc
	&:after
		border-color: #ccc
.border-bottom
	&:before
		border-color: #ccc
	&:after
		border-color: #ccc
.list
	overflow: hidden
	position: absolute
	top: 1.58rem
	left: 0
	right: 0
	bottom: 0
	.title
		line-height: .54rem
		background: #eee
		padding-left: .2rem
		color: #666
		font-size: .26rem
	.button-list
		overflow: hidden
		padding: .1rem .6rem .1rem .1rem
		.button-wrapper
			float: left
			width: 33.33%
			.button
				margin: .1rem
				padding: .1rem 0
				text-align: center
				border: .02rem solid #ccc
				border-radius: .06rem
	.item-list
		.item
			line-height: .76rem
			padding-left: .2rem
</style>
